package com.chisheng.caa.pojo.response;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chisheng.caa.util.BeanUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Collection;
import java.util.stream.Collectors;

@Data
@ApiModel("分页数据传输对象")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PageDTO<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("当前页")
    private Long current;

    @ApiModelProperty("页大小")
    private Long size;

    @ApiModelProperty("总页数")
    private Long total;

    @ApiModelProperty("页数据")
    private Collection<T> records;

    /**
     * 从 Page 实例构造 PageDTO
     */
    public static <T> PageDTO<T> getInstance(Page<?> page, Class<T> dtoClazz) {
        PageDTO<T> pageDTO = new PageDTO<>();
        BeanUtil.copyProperties(page, pageDTO);
        pageDTO.setRecords(page.getRecords().stream()
                .map(e -> BeanUtil.copyProperties(e, dtoClazz))
                .collect(Collectors.toList()));
        return pageDTO;
    }

}
